<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>






<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
    
  
  <link href="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






  

<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.2/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

<link href="/lh706.github.io/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="JavaScript," />








  <link rel="shortcut icon" type="image/x-icon" href="/lh706.github.io/favicon.ico?v=5.1.1" />






<meta name="description" content="ajax使用XHR一级">
<meta name="keywords" content="JavaScript">
<meta property="og:type" content="article">
<meta property="og:title" content="AJAX,XML,JSON">
<meta property="og:url" content="https://lh706.github.io/lh706.github.io/2017/05/20/AJAX-XML-JSON/index.html">
<meta property="og:site_name" content="Albert的blog">
<meta property="og:description" content="ajax使用XHR一级">
<meta property="og:updated_time" content="2017-05-20T04:10:10.964Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="AJAX,XML,JSON">
<meta name="twitter:description" content="ajax使用XHR一级">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/lh706.github.io/',
    scheme: 'Mist',
    sidebar: {"position":"right","display":"always","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://lh706.github.io/lh706.github.io/2017/05/20/AJAX-XML-JSON/"/>





  <title>AJAX,XML,JSON | Albert的blog</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/lh706.github.io/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Albert的blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/lh706.github.io/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/lh706.github.io/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/lh706.github.io/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-tags " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://lh706.github.io/lh706.github.io/lh706.github.io/2017/05/20/AJAX-XML-JSON/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Albert">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/lh706.github.io/my.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Albert的blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">AJAX,XML,JSON</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-20T11:41:57+08:00">
                2017-05-20
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="ajax使用"><a href="#ajax使用" class="headerlink" title="ajax使用"></a>ajax使用</h3><p>XHR一级<br><a id="more"></a><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div></pre></td><td class="code"><pre><div class="line"><span class="number">1.</span>创建一个ajax对象--request;</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">createrequest</span>(<span class="params"></span>)</span>&#123;</div><div class="line">    <span class="keyword">try</span>&#123;request=<span class="keyword">new</span> XMLHttpRequest();&#125;</div><div class="line">    <span class="keyword">catch</span>(a)&#123;</div><div class="line">    <span class="keyword">try</span>&#123;request=<span class="keyword">new</span> ActiveXObject(<span class="string">'Microsoft.XMLHTTP'</span>);&#125; --- 为了兼容IE6及以下 , 现在可以不用考虑</div><div class="line">    <span class="keyword">catch</span>(failed)&#123;</div><div class="line">        request=<span class="literal">null</span>;</div><div class="line">    &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> request;</div><div class="line">&#125;</div><div class="line">直接使用 <span class="keyword">new</span> XMLHttpRequest();</div><div class="line"></div><div class="line"><span class="number">2.</span>处理数据</div><div class="line">GET:</div><div class="line">URL处理---</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">URLserialize</span>(<span class="params">url,name,value</span>)</span>&#123;</div><div class="line">    url+=(url.indexOf(<span class="string">"?"</span>)==<span class="number">-1</span>?<span class="string">"?"</span>:<span class="string">"&amp;"</span>);</div><div class="line">    url+=name;</div><div class="line">    url+=<span class="string">"="</span>+value;</div><div class="line">    <span class="keyword">return</span> url;</div><div class="line">&#125;;</div><div class="line">接受---$_GET[<span class="string">'name'</span>]</div><div class="line">POST:</div><div class="line"><span class="keyword">var</span> a=&#123;...&#125;;---对象a;</div><div class="line"><span class="keyword">var</span> b=<span class="built_in">JSON</span>.stringify(a);---将a转为json;</div><div class="line">data=<span class="string">"data="</span>+b;</div><div class="line">或者</div><div class="line"><span class="keyword">var</span> data=<span class="string">"Name=Bill&amp;LastName=Gates"</span>;</div><div class="line">    request.setRequestHeader(<span class="string">"Content-Type"</span>,<span class="string">"application/x-www-form-urlencoded"</span>);</div><div class="line">读取---设置http表头,使提交和提交表单类似,才能通过$_post[<span class="string">'data/name'</span>]来读取;</div><div class="line"></div><div class="line"><span class="number">3.</span>发送数据--通过request对象的两个方法</div><div class="line">requset.open(<span class="string">"GET/PSOT"</span>,<span class="string">"URL"</span>,<span class="literal">true</span>/<span class="literal">false</span>);</div><div class="line">设置报头</div><div class="line">request.send(<span class="literal">null</span>/post_data);</div><div class="line"></div><div class="line"><span class="number">4.</span>接受数据--一个事件,<span class="number">5</span>个属性</div><div class="line">request.onreadystatechange=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</div><div class="line">    <span class="keyword">if</span>(request.readyState===<span class="number">4</span>)&#123;</div><div class="line">        <span class="keyword">if</span>(request.status&gt;=<span class="number">200</span>&amp;&amp;request.status&lt;<span class="number">300</span>||request.status===<span class="number">304</span>)&#123;</div><div class="line">            data=request.responseText;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">事件:readystatechange---readyState改变就会触发;</div><div class="line">属性方法:</div><div class="line"><span class="number">1.</span>readyState--服务器状态码 </div><div class="line">    未调用open<span class="number">-0</span>;</div><div class="line">    准备发送<span class="number">-1</span>;</div><div class="line">    服务器正在处理<span class="number">-2</span>;</div><div class="line">    值已经返回,但是还不能使用<span class="number">-3</span>;</div><div class="line">    准备就绪<span class="number">-4</span>;</div><div class="line"><span class="number">2.</span>status--http协议状态码;</div><div class="line"><span class="number">3.</span>statusText---状态说明;</div><div class="line"><span class="number">4.</span>resposeXML---如果返回类型是xml,则保存在这里;</div><div class="line"><span class="number">5.</span>responseText---接受返回的文本;</div><div class="line"><span class="number">6.</span>getAllResponseHeaders()---获得所有响应报头</div><div class="line"><span class="number">7.</span>getResponseHeader()---获得响应的某个字段的值</div></pre></td></tr></table></figure></p>
<p>关于跨域:IE8就有XDR类型来跨域;<br>ie10与其他浏览器可以使用绝对地址来实现跨域,只是不能自定义信息头与接受发送cookie;</p>
<h3 id="jsonp"><a href="#jsonp" class="headerlink" title="jsonp"></a>jsonp</h3><p>动态生成请求js文件<code>&lt;script src=&quot;xxx&quot;&gt;&lt;script&gt;</code> ,服务器端通过动态生成的js文件来实现跨域传递数据;</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> cat = <span class="function"><span class="keyword">function</span>(<span class="params">food</span>)</span>&#123;</div><div class="line">    alert(<span class="string">"cat eat"</span>+food)</div><div class="line">&#125;;</div><div class="line"><span class="keyword">var</span> url = <span class="string">"服务器地址?food=fish&amp;callback=回调函数名cat"</span>;</div><div class="line">服务器端通过$_GET[<span class="string">"callback"</span>]获取回调函数名,然后返回js文件中执行cat(value);</div><div class="line"></div><div class="line"><span class="keyword">var</span> script = <span class="built_in">document</span>.createElement(<span class="string">'script'</span>);</div><div class="line">script.setAttribute(<span class="string">'src'</span>,url);</div><div class="line"><span class="built_in">document</span>.getElementsByTagName(<span class="string">'head'</span>)[<span class="number">0</span>].appendChild(script);</div><div class="line">建立了一个请求,然后服务器端返回json数据;</div></pre></td></tr></table></figure>
<h3 id="json"><a href="#json" class="headerlink" title="json"></a>json</h3><p>序列化:<br>    var a=JSON.stringify(b,过滤器,缩进)<br>    过滤器:<br>    数组–只保留数组中的值的属性的字符串<br>    函数–接受key与value,然后返回处理后的value;<br>    缩进:<br>    数值–缩进的空格数<br>    字符–转义字符或者其他字符<br>值为函数或者undefined会被省略<br>解析:<br>JSON.parse();<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> a=<span class="string">'function()&#123;console.info(1);&#125;'</span></div><div class="line"><span class="keyword">var</span> js=&#123;</div><div class="line">    <span class="attr">name1</span>:<span class="string">"lh1"</span>,</div><div class="line">    <span class="attr">name2</span>:a,</div><div class="line">    <span class="attr">name3</span>:[<span class="string">'1'</span>,<span class="number">2</span>,<span class="number">3</span>],</div><div class="line">    <span class="string">"name4"</span>:&#123;</div><div class="line">        <span class="attr">name5</span>:<span class="string">'lh2'</span>,</div><div class="line">        <span class="attr">name6</span>:<span class="string">'lh3'</span></div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"><span class="keyword">var</span>  json=<span class="built_in">JSON</span>.stringify(js,<span class="function"><span class="keyword">function</span>(<span class="params">key,value</span>)</span>&#123;</div><div class="line">    <span class="keyword">if</span>(key===<span class="string">'name1'</span>)&#123;</div><div class="line">        <span class="keyword">return</span> <span class="string">'lh'</span>;</div><div class="line">    &#125;<span class="keyword">else</span>&#123;</div><div class="line">        <span class="keyword">return</span> value;</div><div class="line">    &#125;</div><div class="line">&#125;,<span class="number">1</span>);</div><div class="line"><span class="keyword">var</span> js1=<span class="built_in">JSON</span>.parse(json);</div><div class="line"></div><div class="line">js1=&#123;<span class="attr">name1</span>: <span class="string">"lh"</span>, <span class="attr">name3</span>: <span class="built_in">Array</span>[<span class="number">3</span>], <span class="attr">name4</span>: <span class="built_in">Object</span>&#125;</div><div class="line">函数被省略,通过过滤器,改变了name1;</div></pre></td></tr></table></figure></p>
<h3 id="XML"><a href="#XML" class="headerlink" title="XML"></a>XML</h3><p>1.xml是用来传输数据,html是显示数据<br>2.xml不会作任何事,只是结构化,存储和传输数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">1.必须要一个根元素</div><div class="line">2.属性值必须加引号</div><div class="line">3.空格会被保留</div><div class="line">4.正确嵌套</div><div class="line">5.必须关闭</div><div class="line">6.注释和html中一样</div><div class="line">7.区分大小写</div><div class="line">8.命名规则:</div><div class="line">    字母开头,然后可以加数字,符号等,不能带空格和以xml开始;</div></pre></td></tr></table></figure>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/lh706.github.io/tags/JavaScript/" rel="tag"># JavaScript</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/lh706.github.io/2017/05/20/对象拷贝与继承/" rel="next" title="对象拷贝与继承">
                <i class="fa fa-chevron-left"></i> 对象拷贝与继承
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/lh706.github.io/2017/05/20/html-css相关/" rel="prev" title="html-css相关">
                html-css相关 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/lh706.github.io/my.jpg"
               alt="Albert" />
          <p class="site-author-name" itemprop="name">Albert</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
           
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/lh706.github.io/archives">
                <span class="site-state-item-count">20</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/lh706.github.io/tags/index.html">
                <span class="site-state-item-count">11</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/lh706" target="_blank" title="GitHub">
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.zhihu.com/people/liang-da-shu-shu" target="_blank" title="知乎">
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#ajax使用"><span class="nav-number">1.</span> <span class="nav-text">ajax使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#jsonp"><span class="nav-number">2.</span> <span class="nav-text">jsonp</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#json"><span class="nav-number">3.</span> <span class="nav-text">json</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#XML"><span class="nav-number">4.</span> <span class="nav-text">XML</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Albert</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  <script type="text/javascript" src="//cdn.jsdelivr.net/jquery/2.1.3/jquery.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/fastclick/1.0.6/fastclick.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazyload/1.9.3/jquery.lazyload.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.ui.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.pack.js"></script>

  
  <script type="text/javascript" src="/lh706.github.io/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/lh706.github.io/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/lh706.github.io/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/lh706.github.io/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/lh706.github.io/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/lh706.github.io/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

</body>
</html>
